package com.yan.demo;

import java.util.Arrays;

// 冒泡排序优化
public class Test15 {
    public static void main(String[] args) {
        int[] arr = {5, 12, 90, 18, 77, 76, 45, 28, 59, 72};
        bubbleSort(arr);
        /*int[] copyOfRange = MyArrays.copyOfRange(arr, 2, 5);
        for (int yuanSu : copyOfRange) {
            System.out.print(yuanSu + " ");
        }*/
    }

    public static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            Boolean flag = true; // 设置flag当有一轮循环没有改变flag则排序完毕直接跳出
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    flag = false;
                }
            }

            if (flag) {
                break;
            }
        }
        System.out.println(Arrays.toString(arr));
    }
}
